package com.ruoyi.quartz.task;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mespro.domain.AlldataSizing;
import com.ruoyi.mespro.domain.AlldataWarping;
import com.ruoyi.mespro.domain.ShaftSize;
import com.ruoyi.mespro.domain.ShaftWarp;
import com.ruoyi.mespro.service.IAlldataSizingService;
import com.ruoyi.mespro.service.IAlldataWarpingService;
import com.ruoyi.mespro.service.IShaftSizeService;
import com.ruoyi.mespro.service.IShaftWarpService;
import com.ruoyi.quartz.service.ISysJobLogService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */

@Component("ryTask")
public class RyTask
{
    @Autowired
    private IShaftWarpService shaftWarpService;
    @Autowired
    private IAlldataWarpingService alldataWarpingService;
    @Autowired
    private IAlldataSizingService alldataSizingService;
    @Autowired
    private IShaftSizeService shaftSizeService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysJobLogService jobLogService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }


    public void autoSepWarping() throws ParseException {
        jobLogService.cleanJobLog(); // 换班自动清空日志；
        List<AlldataWarping> alldataWarpings = alldataWarpingService.selectAlldataWarpingList(new AlldataWarping());
        List<SysDictData> data = dictTypeService.selectDictDataByType("dict_shiftType");
        SysDictData sysDictData = DateUtils.parseHourToShiftType(DateUtils.addMinutes(new Date(), 1), data);
        for (AlldataWarping alldataWarping : alldataWarpings) {
            if (alldataWarping.getZzno() != null){
                ShaftWarp shaftWarp = shaftWarpService.selectShaftWarpByShaftCode(alldataWarping.getZzno());
                shaftWarp.setLjtime(new Date());
                shaftWarp.setIscrossshift(1);
                shaftWarp.setBeamstatus(1);
                shaftWarp.setAlldataWarping(alldataWarping);
                shaftWarpService.updateShaftWarp(shaftWarp);

                ShaftWarp newshaftwarp = new ShaftWarp();
                newshaftwarp.setShifttype(Integer.parseInt(sysDictData.getDictValue()));
                newshaftwarp.setShiftdate(sysDictData.getCreateTime());
                newshaftwarp.setBeamstatus(0);
                newshaftwarp.setMaccode(shaftWarp.getMaccode());
                newshaftwarp.setIscrossshift(1);
                newshaftwarp.setSjtime(new Date());
                newshaftwarp.setOrdercode(shaftWarp.getOrdercode());
                newshaftwarp.setProductArea(shaftWarp.getProductArea());
                newshaftwarp.setBatchNo(shaftWarp.getBatchNo());
                newshaftwarp.setColorNo(shaftWarp.getColorNo());
                newshaftwarp.setShaftcode(shaftWarp.getShaftcode());
                newshaftwarp.setPdtcode(shaftWarp.getPdtcode());
                newshaftwarp.setOpworker(shaftWarp.getOpworker());
                newshaftwarp.setConstruction(shaftWarp.getConstruction());
                shaftWarpService.insertShaftWarp(newshaftwarp);
            }

        }
        List<AlldataSizing> alldataSizings = alldataSizingService.selectAlldataSizingList(new AlldataSizing());
        for (AlldataSizing alldataSizing: alldataSizings) {
            if (alldataSizing.getZzno() != null){
                ShaftSize shaftSize = shaftSizeService.selectShaftSizeByShaftCode(alldataSizing.getZzno());
                shaftSize.setLjtime(new Date());
                shaftSize.setIscrossshift(1);
                shaftSize.setBeamstatus(1);
                shaftSize.setAlldataSizing(alldataSizing);
                shaftSizeService.updateShaftSize(shaftSize);

                ShaftSize newshaftSize = new ShaftSize();
                newshaftSize.setShifttype(Integer.parseInt(sysDictData.getDictValue()));
                newshaftSize.setShiftdate(sysDictData.getCreateTime());
                newshaftSize.setBeamstatus(0);
                newshaftSize.setMaccode(shaftSize.getMaccode());
                newshaftSize.setBatchNo(shaftSize.getBatchNo());
                newshaftSize.setJarNo(shaftSize.getJarNo());
                newshaftSize.setProductArea(shaftSize.getProductArea());
                newshaftSize.setColorNo(shaftSize.getColorNo());
                newshaftSize.setIscrossshift(1);
                newshaftSize.setSjtime(new Date());
                newshaftSize.setConstruction(shaftSize.getConstruction());
                newshaftSize.setOrdercode(shaftSize.getOrdercode());
                newshaftSize.setShaftcode(shaftSize.getShaftcode());
                newshaftSize.setPdfcode(shaftSize.getPdfcode());
                newshaftSize.setOpworker(shaftSize.getOpworker());
                shaftSizeService.insertShaftSize(newshaftSize);
            }
        }
    }

    // 整经自动落轴
    public void autoWarping() throws ParseException {

        // 查询所有在织轴
        ShaftWarp shaftWarp = new ShaftWarp();
        List<ShaftWarp> list = shaftWarpService.selectWeavingShaftWarp(shaftWarp);
        if(list.size()>0){
            for(int i=0; i<list.size(); i++){
                shaftWarp = list.get(i);
                // 获得实时表中的长度
                AlldataWarping alldataWarping = new AlldataWarping();
                alldataWarping.setMid(shaftWarp.getMaccode().split("-")[0]);
                alldataWarping.setStation(shaftWarp.getMaccode().split("-")[1]);
                AlldataWarping updateAW = alldataWarpingService.selectAlldataWarpingByMacCode(alldataWarping);

                // 获取旧表中的长度
                AlldataWarping alldataWarpingOld = new AlldataWarping();
                alldataWarpingOld.setMid(shaftWarp.getMaccode().split("-")[0]);
                alldataWarpingOld.setStation(shaftWarp.getMaccode().split("-")[1]);
                AlldataWarping updateAWOld = alldataWarpingService.selectAlldataWarpingOldByMacCode(alldataWarpingOld);
                // 判断旧表中是否有数据 没有无法比较 插入当前数据
                if(updateAWOld == null){
                    alldataWarpingService.insertAlldataWarpingOld(updateAW);
                }else{
                    // 如果实时长度小于旧的长度就执行落轴
                    if(updateAWOld.getCurlength() > updateAW.getCurlength()){

                        shaftWarp.setAlldataWarping(updateAWOld);
                        shaftWarp.setBeamstatus(1);
                        shaftWarp.setLjtime(new Date());
                        if (shaftWarp.getIscrossshift()==1){
                            ShaftWarp lastshaftWarp = shaftWarpService.selectShaftWarpByCodeStatus(shaftWarp.getShaftcode());
                            shaftWarp.setCurlength(updateAWOld.getCurlength()-lastshaftWarp.getCurlength());
                            shaftWarp.setBreaknum(updateAWOld.getBreaknum()-lastshaftWarp.getBreaknum());
                            shaftWarp.setBreakrate(updateAWOld.getBreakrate()-lastshaftWarp.getBreakrate());
                        }

                        shaftWarpService.updateShaftWarp(shaftWarp);
                        alldataWarpingService.updateZZno(updateAW);

                        // 执行完落轴更新旧表中的数据
                        updateAW.setCurlength(0);
                        alldataWarpingService.updateAlldataWarpingOld(updateAW);
                    }else{
                        alldataWarpingService.updateAlldataWarpingOld(updateAW);
                    }

                }

            }
        }
    }

    public void autoSizing() throws ParseException{
        ShaftSize shaftSize =  new ShaftSize();
        List<ShaftSize> list = shaftSizeService.selectWeavingShaftSize(shaftSize);
        if(list.size()>0){
            for(int i=0; i<list.size(); i++){
                shaftSize = list.get(i);
                // 获得实时表中的长度
                AlldataSizing alldataSizing = new AlldataSizing();
                alldataSizing.setMid(shaftSize.getMaccode().split("-")[0]);
                alldataSizing.setStation(shaftSize.getMaccode().split("-")[1]);
                AlldataSizing updateAS = alldataSizingService.selectAlldataSizingByMacCode(alldataSizing);

                // 获取旧表中的长度
                AlldataSizing alldataSizingOld = new AlldataSizing();
                alldataSizingOld.setMid(shaftSize.getMaccode().split("-")[0]);
                alldataSizingOld.setStation(shaftSize.getMaccode().split("-")[1]);
                AlldataSizing updateASOld = alldataSizingService.selectAlldataSizingOldByMacCode(alldataSizingOld);
                // 判断旧表中是否有数据 没有无法比较 插入当前数据
                if(updateASOld == null){
                    alldataSizingService.insertAlldataSizingOld(updateAS);
                }else{
                    // 如果实时长度小于旧的长度就执行落轴
                    if(updateASOld.getBeamlength() > updateAS.getBeamlength()){

                        shaftSize.setAlldataSizing(updateASOld);
                        shaftSize.setBeamstatus(1);
                        shaftSize.setLjtime(new Date());
                        if (shaftSize.getIscrossshift()==1){
                            ShaftSize lastshaftsize = shaftSizeService.selectShaftSizeByCodeStatus(shaftSize.getShaftcode());
                            shaftSize.setBeamlength(updateASOld.getBeamlength()-lastshaftsize.getBeamlength());
                        }
                        alldataSizingService.updateZZno(updateAS);
                        shaftSizeService.updateShaftSize(shaftSize);

                        // 执行完落轴更新旧表中的数据
                        updateAS.setBeamlength((double) 0);
                        alldataSizingService.updateAlldataSizingOld(updateAS);
                    }else {
                        alldataSizingService.updateAlldataSizingOld(updateAS);
                    }
                }

            }
        }
    }

}
